package ltesim.sdkManager.listener;

import ltesim.sdkManager.SDKManager;
import ltesim.sdkManager.sdklist.SDKList;
import ltesim.sdkManager.versionParser.SDKVersionParser;

import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * Add SDK button listener
 * 
 * @author xirka
 * 
 */
public class AddButtonListener implements SelectionListener {
	private Display display;
	private SDKManager sdkManager;

	public AddButtonListener(SDKManager _sdkManager, Display _display) {
		sdkManager = _sdkManager;
		display = _display;
	}

	@Override
	public void widgetSelected(SelectionEvent e) {
		// System.out.println("addButtonListener::widgetSelected");
		Shell shell = new Shell(display);
		DirectoryDialog dialog = new DirectoryDialog(shell);

		dialog.setText("SDK Manager");
		dialog.setMessage("Select the SDK directory root");

		String targetPath = dialog.open();
		String version = SDKVersionParser.getVersion(targetPath);

		SDKList mySDK = sdkManager.getMySDK();
		mySDK.addVersion(version, targetPath);
		mySDK.writeConfig();
		// System.out.println("mySDK");
		sdkManager.clearTableContent();
		sdkManager.setMySDK(mySDK);
		sdkManager.resetTableContent();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}

		display.dispose();

	}

	@Override
	public void widgetDefaultSelected(SelectionEvent e) {
		widgetSelected(e);
	}

}
